package controller;

import java.util.Date;
import java.util.List;

import model.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import business.IUserBussines;

@Controller
public class IndexController {

	@Autowired
	private IUserBussines userBusiness;

	@RequestMapping(value = "/index", method = RequestMethod.GET)
	public String index(ModelMap model) {
		model.addAttribute("data", new Date().toString());
		return "logowaniePage";
	}

	@RequestMapping(value = "/mainPage", method = RequestMethod.GET)
	public String mainPage(@ModelAttribute("model") ModelMap model) {
		List<User> users = userBusiness.getAllUsers();
		model.addAttribute("usersList", users);
		model.addAttribute("test", "wartosc");
		return "mainPage";
	}

	@RequestMapping(value = "/loguj", method = RequestMethod.POST)
	public String logowanie(User user) {

		if (userBusiness.loguj(user)) {
			return "redirect:mainPage.html";
		}

		return "logowaniePage";
	}

}
